package weaver.formmode.customjavacode.modeexpand;

import com.yunzhou.utils.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.monitor.cache.CacheFactory;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2023-02-21
 * @Version 1.0
 * @Description TODO 船体信息编辑的时候删除明细行表示解除组件关联
 **/
public class CancelStatusFormEdit extends AbstractModeExpandJavaCodeNew {
    private LogTool logTool = new LogTool("/log/ctmp/CancelStatusFormEdit", false);
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        logTool.writeLog("进入模块扩展动作>船体铭牌扫码关联组件");
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {
            User user = (User) param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                RecordSet rs1 = new RecordSet();
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    String sql1 = "select * from uf_ctmpxx_dt1 where mainid="+billid;
                    List<String> list1 = new ArrayList<>();//修改后关联的组件id
                    rs1.execute(sql1);
                    while (rs1.next()){
                        list1.add(rs1.getString("zjbm"));
                    }
                    String sql2 = "select * from uf_zjxx where sglct="+billid;
                    rs1.execute(sql2);
                    String updateSql = "update uf_zjxx set zjzt=0,sglct='' where id=?";
                    List<List<Object>> updateList = new ArrayList<>();
                    while (rs1.next()){
                        String id = rs1.getString("id");
                        if(!list1.contains(id)){
                            //表示组件没有关联了 需要更新组件信息表
                            List<Object> values2 = new ArrayList<>();
                            values2.add(id);
                            updateList.add(values2);
                        }
                    }
                    rst.executeBatchSql(updateSql,updateList);

                    CacheFactory.getInstance().clear();
                    CacheFactory.getInstance().reset();

                }else{
                    result.put("errmsg", "1 编辑保存動作擴展接口異常 未获取到id");
                    result.put("flag", "false");
                    return result;
                }
            }else{
                result.put("errmsg", "2 编辑保存動作擴展接口異常 未获取requestinfo对象");
                result.put("flag", "false");
                return result;
            }
        } catch (Exception e) {
            rst.rollback();
            e.printStackTrace();
            result.put("errmsg", "3 编辑保存動作擴展接口異常 \n" + e.getMessage());
            result.put("flag", "false");
            return result;
        }
        rst.commit();
        return result;
    }

}
